package com.bezdelnick.forum.mail.type;

import com.bezdelnick.forum.domain.Question;
import com.bezdelnick.forum.service.ForumService;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 20.07.12
 * Time: 11:54
 * To change this template use File | Settings | File Templates.
 */
public class NotificationQuestionMail extends AbstractMail {
    private static final Logger LOGGER = LoggerFactory.getLogger(NotificationQuestionMail.class);

    private static final String NAME_LETTER = "notification_question";

    @SpringBean
    @SuppressWarnings("unused")
    public ForumService forumService;

    public NotificationQuestionMail() {
        /* Add this instance to Spring context */
        SpringComponentInjector.get().inject(this);
    }

    @Override
    public String getTemplateName() {
        return NAME_LETTER;
    }

    @Override
    public boolean buildContext(Object object) {
        Question question = null;

        try {
            question = (Question) object;

            this.context = new HashMap();
            try {
                this.context.put("username", question.getUserId() == 0 ? 0 : forumService.getCurrentUser().getUsername());
                this.context.put("title", question.getTitle() == null ? "_____" : question.getTitle());
                this.context.put("date", question.getDatePublication() == null ? new Date() : question.getDatePublication());
                return true;
            } catch (Exception e) {
                LOGGER.error(e.toString());
                LOGGER.error("It can not get mail context");
                return false;
            }
        } catch (Exception e) {
            LOGGER.warn(e.getMessage());
            return false;
        }
    }
}
